package Controller;

import Service.UserOpService;
import com.sun.deploy.net.HttpRequest;
import models.User;
import models.daily;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * Created by 郑凯 on 2016/6/20.
 */
@Controller
public class UserOp {
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private UserOpService userOpService;
    @RequestMapping("submitBlog")
    public String submitBlog(@RequestParam("title") String title,@RequestParam("tab") String tab,@RequestParam("content")String content)
    {

        if(httpSession.getAttribute("uid") != null)
        {
            if(title.equals("")) title = content.substring(0,8);
            userOpService.saveDaily(title,content,tab, (String) httpSession.getAttribute("uid"));
            return "redirect:list";
        }
        return "redirect:list";
    }
    @RequestMapping("modifyBlog")
    public String modifyBlog(@RequestParam("did")String did,@RequestParam("title") String title,@RequestParam("tab") String tab,@RequestParam("content")String content)
    {

        userOpService.updateDailyByDid(did,title,content,tab);
        return "redirect:list";
    }
    @RequestMapping("deleteDaily")
    public String deleteDaily(@RequestParam("did") String did)
    {
        userOpService.deleteDailyByDid(did);
        String uid = (String)httpSession.getAttribute("uid");
        userOpService.updateUserDaily(uid);
        return "redirect:Manager?pid=0";
    }
    @RequestMapping("preview")
    public ModelAndView preview(@RequestParam("title") String title,@RequestParam("content")String content,@RequestParam("tab") String tab)
    {
        ModelAndView modelAndView = new ModelAndView("info");

        if (httpSession.getAttribute("uid") != null) {
            User user = userOpService.getUserByUid((String) httpSession.getAttribute("uid"));
            daily d = new daily();
            d.setTab(tab);
            d.setTitle(title);
            d.setContent(content);
            d.setTime("XXXX-XX-XX:XX-XX-XX");
            modelAndView.addObject("user", user);
            modelAndView.addObject("daily", d);
            return modelAndView;
        } else {
            return new ModelAndView("login","tip","请先登录");
        }
    }
    @RequestMapping("modifyUserInfo")
    public ModelAndView  modifyUser(@RequestParam("file")MultipartFile multipartFile, @RequestParam("name") String name, @RequestParam("password") String password)
    {
        String uid = (String)httpSession.getAttribute("uid");
        String imageName = multipartFile.getOriginalFilename();
        String imagePath = httpSession.getServletContext().getRealPath("/");


        User user = userOpService.getUserByUid(uid);
        if(name.equals("")) name = user.getName();
        if(password.equals("")) password = user.getPassword();

        if(!imageName.equals(""))
        {
            File file = new File(imagePath+"blog//uploadImg",imageName);

            try {
                multipartFile.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            userOpService.modifyUserInfo(uid,name,password,"uploadImg/"+imageName);
        }

        return new ModelAndView("login","tip","修改成功，请重新登录");
    }

}
